package com.example.nacos.controller;

import com.example.nacos.entity.Order;
import com.example.nacos.entity.Product;
import com.example.nacos.mapper.OrderMapper;
import com.example.nacos.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 多数据源演示控制器
 */
@RestController
@RequestMapping("/demo")
@Slf4j
public class DataSourceDemoController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;


    /**
     * 获取数据源状态
     * @return 数据源状态
     */
    @GetMapping("/status")
    public Map<String, String> getDataSourceStatus() {
        Map<String, String> status = new HashMap<>();
        status.put("master", "online");
        status.put("slave1", "online");
        status.put("slave2", "online");
        status.put("description", "多数据源配置正常");
        Order order = orderMapper.selectById("1");
        log.info("查询订单信息：{}", order);
        System.out.println( order);
        status.put("order", order.toString());

        Product product = productMapper.selectById("3");
        System.out.println( product);
        status.put("product", product.toString());
        return status;
    }

}